/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jama.carouselviewexample.adapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.jama.carouselviewexample.ResourceTable;
import com.jama.carouselviewexample.bean.MovieBean;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.agp.text.Font;

import java.util.List;

/**
 * StartTallListProvider
 *
 * @since 2021-05-20
 */
public class StartTallListProvider extends BaseItemProvider {
    private static final int NUM_20 = 20;
    private List<MovieBean> list;
    private AbilitySlice slice;

    /**
     * StartTallListProvider
     *
     * @param movieBeanList movieBeanList
     * @param abilitySlice  slice
     */
    public StartTallListProvider(List<MovieBean> movieBeanList, AbilitySlice abilitySlice) {
        this.list = movieBeanList;
        this.slice = abilitySlice;
    }

    /**
     * getCount
     *
     * @return int
     */
    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * getItem
     *
     * @param position position
     * @return Object
     */
    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()) {
            return list.get(position);
        }
        return null;
    }

    /**
     * getItemId
     *
     * @param position getItemId
     * @return long
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * getComponent
     *
     * @param position           position
     * @param component          component
     * @param componentContainer componentContainer
     * @return Component
     */
    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        StartTallListProvider.MovieHolder holder;
        MovieBean movieBean = list.get(position);
        if (component == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_start_layout_tall,
                    null, false);
            holder = new StartTallListProvider.MovieHolder(cpt);
            // 将获取到的子组件信息绑定到列表项的实例中
            cpt.setTag(holder);
        } else {
            cpt = component;
            // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
            holder = (MovieHolder) cpt.getTag();
        }
        Glide.with(slice.getContext())
                .load(movieBean.getmovieId())
                .centerCrop()
                .transform(new RoundedCorners(NUM_20))
                .into(holder.movieIma);
        holder.movieText.setText(movieBean.getMovieName());
        holder.movieText.setFont(Font.DEFAULT_BOLD);
        holder.starIma.setPixelMap(movieBean.getStartImage());

        return cpt;
    }

    /**
     * MovieHolder
     *
     * @since 2021-05-20
     */
    static class MovieHolder {
        private Image movieIma;
        private Image starIma;
        private Text movieText;

        MovieHolder(Component component) {
            movieIma = (Image) component.findComponentById(ResourceTable.Id_iv_start_image_tall);
            starIma = (Image) component.findComponentById(ResourceTable.Id_iv_start_tall);
            movieText = (Text) component.findComponentById(ResourceTable.Id_tv_layout_start_tall);
        }

        public Image getMovieIma() {
            return movieIma;
        }

        public void setMovieIma(Image img) {
            this.movieIma = img;
        }

        public Image getStarIma() {
            return starIma;
        }

        public void setStarIma(Image img) {
            this.starIma = img;
        }

        public Text getMovieText() {
            return movieText;
        }

        public void setMovieText(Text text) {
            this.movieText = text;
        }
    }

}
